package com.example.controller;

import com.example.common.Result;
import com.example.entity.Order;
import com.example.entity.PageBean;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/list")
    public Result getOrderList(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String query){
        PageBean orderList= orderService.orderList(page,pageSize,query);
        return Result.success(orderList);
    }

    @GetMapping("/listByCustomerId")
    public Result getOrderListById(@RequestParam String customerId){
        List<Order> orderListById= orderService.orderListById(customerId);
        return Result.success(orderListById);
    }

    @GetMapping("/info")
    public Result getuOrderidinfo(@RequestParam("order_id") String order_id)
    {
        Order order=orderService.findByOrderid(order_id);
        return Result.success(order);
    }
    @GetMapping("/infoTableId")
    public Result getOrderTable_id(@RequestParam("table_id") String table_id)
    {
        Order order=orderService.findByOrderTableid(table_id);
        return Result.success(order);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Order order) throws ParseException {
        int newOrderId = orderService.insertOrder(order);
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("orderId", newOrderId);
        return Result.success(responseData);
    }
    @PutMapping("/updata")
    public Result updata(@RequestBody Order order){
        orderService.updata(order);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("order_id") String order_id) {
        orderService.delete(order_id);
        return Result.success();
    }

    @GetMapping("/listByIdNo")
    public Result orderListByIdNo(@RequestParam String customerId) {
        List<Order> orderListById = orderService.orderListByIdNo(customerId);
        return Result.success(orderListById);
    }
    @GetMapping("/checkOrders")
    public Result checkOrders() {
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 计算10秒前的时间
        LocalDateTime fiveSecondsAgo = currentTime.minusSeconds(5);
        // 查询在过去10秒内创建的订单
        return Result.success(orderService.getOrdersInTimeRange(fiveSecondsAgo, currentTime));
    }
}
